<template>
<h1>注册教师用户</h1>
  <!-- <input type="button" value="点" @click="chaxun" /> -->
  <el-form
    :model="form"
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="用户名" prop="yonghu">
      <el-input v-model="form.yonghu"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="mima">
      <el-input v-model="form.mima" type="password"></el-input>
    </el-form-item>
    <el-form-item label="教师编号" prop="t_bianhao">
      <el-input v-model="form.t_bianhao" :maxlength="6"></el-input>
    </el-form-item>
    <el-form-item style="text-align: center;">
      <el-button type="primary" @click="zengjia('ruleForm')"
        >立即注册</el-button
      >
      <el-button @click="tiaozhuan">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
const axios = require("axios");
export default {
  data() {
    return {
      form: {
        yonghu: "",
        mima: "",
        t_bianhao: "",
      },
      rules: {
        yonghu: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 9,
            message: "长度在 3 到 9 个字符",
            trigger: "blur",
          },
        ],
        mima: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 9,
            message: "长度在 3 到 9 个字符",
            trigger: "blur",
          },
        ],
        t_bianhao: [
          { required: true, message: "请输入教师编号", trigger: "blur" },
          {
            min: 6,
            max: 6,
            message: "长度是 6 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    onSubmit() {
      console.log("submit!");
    },
    tiaozhuan() {
      this.$router.push({ name: "Home" });
    },
    // chaxun() {
    //   let _this = this;
    //   axios
    //     .get("https://localhost:44391/api/zhuce/chaxunbianhao", {
    //       params: {
    //         t_bianhao: this.form.t_bianhao,
    //       },
    //     })
    //     .then(function (response) {
    //       console.log(response);
    //       debugger;
    //       if (response.data === "") {
    //         _this.$notify({
    //           title: "警告",
    //           message: "未查询到教师编号，请检查教师编号",
    //           type: "warning",
    //         });
    //       }
    //       return response;
    //     });
    // },
    zengjia(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let _this = this;
          // this.form.t_bianhao = parseInt(this.form.t_bianhao);
          console.log(this.form);
          axios
            .get("https://localhost:44391/api/zhuce/chaxunbianhao", {
              params: {
                t_bianhao: this.form.t_bianhao,
              },
            })
            .then(function (response) {
              console.log(response);
              debugger;
              if (response.data === "") {
                _this.$notify({
                  title: "警告",
                  message: "未查询到教师编号，请检查教师编号",
                  type: "warning",
                });
              }
              if (response.data != "") {
                axios
                  .post(
                    "https://localhost:44391/api/denglu/zhucejiaoshi",
                    _this.form
                  )
                  .then(function (response) {
                    console.log(response);
                    if (response.data >= 1) {
                      _this.$notify({
                        title: "成功",
                        message: "注册成功",
                        type: "success",
                      });
                      // _this.dialogFormVisible = false;
                      _this.$router.push({ name: "Home" });
                    } else {
                      _this.$notify({
                        title: "警告",
                        message: "注册失败",
                        type: "warning",
                      });
                    }
                  });
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
h1 {
  font-size: 56px;
  text-align: center;
}
</style>